import http from '@/http/httpClient'
import { useUserStore } from '@/store'

/* 登录 获取 accessToken */
export const loginApi = async data => {
  const res: any = await http.post('/api/user/login', {
    data
  })
  const store = useUserStore()
  store.setUserInfo({
    refreshToken: res.data.token,
    accessToken: res.data.token
  })

  const user: any = await getUserApi()
  store.setUserInfo({ ...user.data, refreshToken: res.data.token, accessToken: res.data.token })
}

/**
 *注册
 * @returns
 */

export const registerApi = async data => {
  const res: any = await http.post('/api/user/register', {
    data
  })
  return res
}

/* 拿 refreshToken 换取 accessToken 与 新 refreshToken */
// /* 即刷新 accessToken */
// export const refreshTokenApi = async () => {
//   const store = useUserStore()
//   const { refreshToken } = store.userInfo || {}
//   const res: any = await http.get('/refresh', {
//     data: {
//       refreshToken: refreshToken
//     }
//   })
//   store.setUserInfo({
//     refreshToken: res.data.refreshToken,
//     accessToken: res.data.accessToken
//   })
//   return res
// }

/* 获取用户信息 */
export const getUserApi = async (data?: any) => {
  return http.get('/api/user/detail', { data })
}
/* 编辑用户信息 */
export const setUserApi = async data => {
  return http.post('/api/user/updateStaffInfo', { data })
}
/* 编辑用户信息 */
export const setUserContactApi = async data => {
  return http.post('/api/user/contact', { data })
}
